package com.priusoft.api.common;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;
import java.util.regex.Pattern;

public class StringUtil {
    private static final String EmailPattern = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$";

    private static final transient Pattern emailPattern;

    private static final String DEFAULT_PREFIX = "${";

    private static final String DEFAULT_SUFFIX = "}";

    static final String[] SBC = new String[] {
            ", ", ", ", ", ", ", ", ", ",
            ", "};

    static final String[] DBC = new String[] {
            ",", ".", ";", "\"", "\"", "?", "!", "(", ")", ":",
            "_", "," };

    private static final int PAD_LIMIT = 8192;

    private static final String[] PADDING;

    private static final String[] EMPTY_STRING_ARRAY;

    static {
        emailPattern = Pattern.compile("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$");
        PADDING = new String[65535];
        EMPTY_STRING_ARRAY = new String[0];
    }

    public static boolean equalsIgnoreCase(String str1, String str2) {
        return (str1 == null) ? ((str2 == null)) : str1.equalsIgnoreCase(str2);
    }

    public static int indexOf(String str, char searchChar) {
        if (isEmpty(str))
            return -1;
        return str.indexOf(searchChar);
    }

    public static boolean isWord(String str) {
        if (str == null)
            return false;
        char[] ch = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isLetterOrDigit(ch[i]) && ch[i] != '_')
                return false;
        }
        return true;
    }

    public static boolean isNumEx(String str) {
        if (str == null || str.length() <= 0)
            return false;
        char[] ch = str.toCharArray();
        for (int i = 0, comcount = 0; i < str.length(); i++) {
            if (!Character.isDigit(ch[i])) {
                if (ch[i] != '.')
                    return false;
                if (i == 0 || i == str.length() - 1)
                    return false;
                if (++comcount > 1)
                    return false;
            }
        }
        return true;
    }

    public static Object getStringNumber(String str, int index) {
        if (str == null)
            return null;
        char[] ch = str.toCharArray();
        String tempStr = "";
        Vector<String> returnNumber = new Vector();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(ch[i])) {
                tempStr = tempStr + ch[i];
            } else {
                if (!tempStr.equals(""))
                    returnNumber.addElement(tempStr);
                tempStr = "";
            }
        }
        if (!tempStr.equals(""))
            returnNumber.addElement(tempStr);
        if (returnNumber.isEmpty() || index > returnNumber.size())
            return null;
        if (index <= 0)
            return returnNumber;
        return returnNumber.elementAt(index - 1);
    }

    public static String[] sortByLength(String[] saSource, boolean bAsc) {
        if (saSource == null || saSource.length <= 0)
            return null;
        int iLength = saSource.length;
        String[] saDest = new String[iLength];
        for (int i = 0; i < iLength; i++)
            saDest[i] = saSource[i];
        String sTemp = "";
        int j = 0, k = 0;
        for (j = 0; j < iLength; j++) {
            for (k = 0; k < iLength - j - 1; k++) {
                if (saDest[k].length() > saDest[k + 1].length() && bAsc) {
                    sTemp = saDest[k];
                    saDest[k] = saDest[k + 1];
                    saDest[k + 1] = sTemp;
                } else if (saDest[k].length() < saDest[k + 1].length() && !bAsc) {
                    sTemp = saDest[k];
                    saDest[k] = saDest[k + 1];
                    saDest[k + 1] = sTemp;
                }
            }
        }
        return saDest;
    }

    public static String symbolSBCToDBC(String sSource) {
        if (sSource == null || sSource.length() <= 0)
            return sSource;
        int iLen = (SBC.length < DBC.length) ? SBC.length : DBC.length;
        for (int i = 0; i < iLen; i++)
            sSource = replace(sSource, SBC[i], DBC[i]);
        return sSource;
    }

    public static String symbolDBCToSBC(String sSource) {
        if (sSource == null || sSource.length() <= 0)
            return sSource;
        int iLen = (SBC.length < DBC.length) ? SBC.length : DBC.length;
        for (int i = 0; i < iLen; i++)
            sSource = replace(sSource, DBC[i], SBC[i]);
        return sSource;
    }

    public static boolean isEmailAddress(String str) {
        if (isEmpty(str))
            return false;
        return emailPattern.matcher(str).matches();
    }

    public static String quoteNullString(String s) {
        if (s == null)
            return "Null";
        if (s.trim().length() == 0)
            return "Null";
        return "'" + s.trim() + "'";
    }

    public static char getCharAtPosDefaultZero(String s, int pos) {
        if (s == null)
            return '0';
        if (s.length() <= pos)
            return '0';
        return s.charAt(pos);
    }

    public static String setCharAtPosAppendZero(String s, int pos, char c) {
        String preString;
        String afterString;
        if (s == null)
            s = "";
        while (pos > s.length() - 1)
            s = s + '0';
        if (pos == 0) {
            preString = "";
        } else {
            preString = s.substring(0, pos);
        }
        if (pos == s.length() - 1) {
            afterString = "";
        } else {
            afterString = s.substring(pos + 1);
        }
        return preString + c + afterString;
    }

    public static String fillBlank(String s, int n, boolean isLeft) {
        if (n < 0)
            return s;
        if (isEmpty(s))
            return rightPad("", n, " ");
        if (s.length() >= n)
            return s;
        if (isLeft)
            return leftPad(s, n, " ");
        return rightPad(s, n, " ");
    }

    public static int compareVersion(String version1, String version2) {
        StringTokenizer st1 = new StringTokenizer(version1, ".");
        StringTokenizer st2 = new StringTokenizer(version2, ".");
        ArrayList<String> al1 = new ArrayList();
        ArrayList<String> al2 = new ArrayList();
        while (st1.hasMoreTokens())
            al1.add(st1.nextToken());
        while (st2.hasMoreTokens())
            al2.add(st2.nextToken());
        int size1 = al1.size();
        int size2 = al2.size();
        for (int i = 0; i < size1 && i < size2; i++) {
            int v1 = Integer.parseInt(al1.get(i));
            int v2 = Integer.parseInt(al2.get(i));
            if (v1 > v2)
                return 1;
            if (v1 < v2)
                return -1;
        }
        if (size1 > size2)
            return 1;
        if (size1 < size2)
            return -1;
        return 0;
    }

    public static String deleteAny(String inString, String charsToDelete) {
        if (inString == null || charsToDelete == null)
            return inString;
        StringBuffer out = new StringBuffer();
        for (int i = 0; i < inString.length(); i++) {
            char c = inString.charAt(i);
            if (charsToDelete.indexOf(c) == -1)
                out.append(c);
        }
        return out.toString();
    }

    public static String quote(String str) {
        return (str != null) ? ("'" + str + "'") : null;
    }

    public static Object quoteIfString(Object obj) {
        return (obj instanceof String) ? quote((String)obj) : obj;
    }

    public static String unqualify(String qualifiedName) {
        return unqualify(qualifiedName, '.');
    }

    public static String unqualify(String qualifiedName, char separator) {
        return qualifiedName.substring(qualifiedName.lastIndexOf(separator) + 1);
    }

    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter) {
        return splitArrayElementsIntoProperties(array, delimiter, null);
    }

    public static Properties splitArrayElementsIntoProperties(String[] array, String delimiter, String charsToDelete) {
        if (array == null || array.length == 0)
            return null;
        Properties result = new Properties();
        for (int i = 0; i < array.length; i++) {
            String element = array[i];
            if (charsToDelete != null)
                element = deleteAny(array[i], charsToDelete);
            String[] splittedElement = split(element, delimiter);
            if (splittedElement != null)
                result.setProperty(splittedElement[0].trim(), splittedElement[1].trim());
        }
        return result;
    }

    public static String[] commaDelimitedListToStringArray(String str) {
        return split(str, ",");
    }

    public static Set commaDelimitedListToSet(String str) {
        Set<String> set = new TreeSet();
        String[] tokens = commaDelimitedListToStringArray(str);
        for (int i = 0; i < tokens.length; i++)
            set.add(tokens[i]);
        return set;
    }

    public static String arrayToDelimitedString(Object[] arr, String delim) {
        if (arr == null)
            return "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (i > 0)
                sb.append(delim);
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) {
        if (coll == null)
            return "";
        StringBuffer sb = new StringBuffer();
        Iterator it = coll.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i > 0)
                sb.append(delim);
            sb.append(prefix).append(it.next()).append(suffix);
            i++;
        }
        return sb.toString();
    }

    public static String collectionToDelimitedString(Collection coll, String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    public static String arrayToCommaDelimitedString(Object[] arr) {
        return arrayToDelimitedString(arr, ",");
    }

    public static String collectionToCommaDelimitedString(Collection coll) {
        return collectionToDelimitedString(coll, ",");
    }

    public static String replace(String src, Map props) {
        return replace(src, "${", "}", props);
    }

    public static String replace(String src, String prefix, String suffix, Map props) {
        int len1 = prefix.length();
        int len2 = suffix.length();
        StringBuffer sb = new StringBuffer();
        int index1 = src.indexOf(prefix);
        while (index1 >= 0) {
            sb.append(src.substring(0, index1));
            src = src.substring(index1 + len1);
            if (src.startsWith(prefix)) {
                sb.append(prefix);
                break;
            }
            int index2 = src.indexOf(suffix);
            if (index2 >= 0) {
                String t = src.substring(0, index2);
                Object o = props.get(t);
                String sp = (o == null) ? "" : o.toString();
                sb.append(sp);
                src = src.substring(index2 + len2);
                index1 = src.indexOf(prefix);
                continue;
            }
            sb.append(prefix);
        }
        sb.append(src);
        return new String(sb);
    }

    public static boolean isNotNullAndBlank(String str) {
        if (isNullOrBlank(str))
            return false;
        return true;
    }

    public static boolean isNullOrBlank(String str) {
        if (isNull(str) || str.equals("") || str.equals("null"))
            return true;
        return false;
    }

    public static boolean isNull(String str) {
        if (str == null || str.trim().length() == 0)
            return true;
        return false;
    }

    public static boolean isNotNull(String str) {
        if (str == null || str.trim().length() == 0)
            return false;
        if (str.trim().equalsIgnoreCase("null"))
            return false;
        return true;
    }

    public static String ifNullToBlank(String str) {
        if (isNotNull(str) && !str.trim().equals("null"))
            return str.trim();
        return "";
    }

    public static String ifNullToBlank(Object obj) {
        String ret = "";
        String s = String.valueOf(obj);
        if (s == null || "".equals(s) || "null".equals(s) || "NULL".equals(s)) {
            ret = "";
        } else {
            ret = s;
        }
        return ret;
    }

    public static boolean hasWildcards(String input) {
        return (contains(input, "*") || contains(input, "?"));
    }

    public static boolean isWildcardMatchOne(String r_Keyword, String[] r_WildcardMatcher, boolean r_CaseSensitive) {
        if (null == r_WildcardMatcher)
            return true;
        for (int i = 0; i < r_WildcardMatcher.length; i++) {
            String t_WildCardMatcher = r_WildcardMatcher[i];
            if (isWildcardMatch(r_Keyword, t_WildCardMatcher, r_CaseSensitive))
                return true;
        }
        return false;
    }

    public static boolean isWildcardMatchAll(String r_Keyword, String[] r_WildcardMatcher, boolean r_CaseSensitive) {
        if (null == r_WildcardMatcher)
            return true;
        for (int i = 0; i < r_WildcardMatcher.length; i++) {
            String t_WildCardMatcher = r_WildcardMatcher[i];
            if (!isWildcardMatch(r_Keyword, t_WildCardMatcher, r_CaseSensitive))
                return false;
        }
        return true;
    }

    public static boolean isWildcardMatch(String r_Keyword, String r_WildcardMatcher) {
        return isWildcardMatch(r_Keyword, r_WildcardMatcher, true);
    }

    public static boolean isWildcardMatch(String r_Keyword, String r_WildcardMatcher, boolean r_CaseSensitive) {
        if (r_Keyword == null && r_WildcardMatcher == null)
            return true;
        if (r_Keyword == null || r_WildcardMatcher == null)
            return false;
        if (!r_CaseSensitive) {
            r_Keyword = r_Keyword.toLowerCase();
            r_WildcardMatcher = r_WildcardMatcher.toLowerCase();
        }
        String[] t_SplitValues = splitOnTokens(r_WildcardMatcher);
        boolean t_Chars = false;
        int t_Index = 0;
        int t_WildIndex = 0;
        Stack<int[]> t_BackStack = new Stack();
        do {
            if (t_BackStack.size() > 0) {
                int[] array = t_BackStack.pop();
                t_WildIndex = array[0];
                t_Index = array[1];
                t_Chars = true;
            }
            while (t_WildIndex < t_SplitValues.length) {
                if (t_SplitValues[t_WildIndex].equals("?")) {
                    t_Index++;
                    t_Chars = false;
                } else if (t_SplitValues[t_WildIndex].equals("*")) {
                    t_Chars = true;
                    if (t_WildIndex == t_SplitValues.length - 1)
                        t_Index = r_Keyword.length();
                } else {
                    if (t_Chars) {
                        t_Index = r_Keyword.indexOf(t_SplitValues[t_WildIndex], t_Index);
                        if (t_Index == -1)
                            break;
                        int repeat = r_Keyword.indexOf(t_SplitValues[t_WildIndex], t_Index + 1);
                        if (repeat >= 0)
                            t_BackStack.push(new int[] { t_WildIndex, repeat });
                    } else if (!r_Keyword.startsWith(t_SplitValues[t_WildIndex], t_Index)) {
                        break;
                    }
                    t_Index += t_SplitValues[t_WildIndex].length();
                    t_Chars = false;
                }
                t_WildIndex++;
            }
            if (t_WildIndex == t_SplitValues.length && t_Index == r_Keyword.length())
                return true;
        } while (t_BackStack.size() > 0);
        return false;
    }

    private static String[] splitOnTokens(String r_Text) {
        if (r_Text.indexOf("?") == -1 && r_Text.indexOf("*") == -1)
            return new String[] { r_Text };
        char[] t_Array = r_Text.toCharArray();
        ArrayList<String> t_List = new ArrayList();
        StringBuffer t_Buffer = new StringBuffer();
        for (int i = 0; i < t_Array.length; i++) {
            if (t_Array[i] == '?' || t_Array[i] == '*') {
                if (t_Buffer.length() != 0) {
                    t_List.add(t_Buffer.toString());
                    t_Buffer.setLength(0);
                }
                if (t_Array[i] == '?') {
                    t_List.add("?");
                } else if (t_List.size() == 0 || (i > 0 && !t_List.get(t_List.size() - 1).equals("*"))) {
                    t_List.add("*");
                }
            } else {
                t_Buffer.append(t_Array[i]);
            }
        }
        if (t_Buffer.length() != 0)
            t_List.add(t_Buffer.toString());
        return t_List.<String>toArray(new String[0]);
    }

    public static boolean isIn(String r_Source, String[] r_Target, boolean r_CaseSensitive) {
        for (int i = 0; i < r_Target.length; i++) {
            String t_Value = r_Target[i];
            if (r_CaseSensitive) {
                if (equals(r_Source, t_Value))
                    return true;
            } else if (equalsIgnoreCase(r_Source, t_Value)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isIn(String r_Source, Collection r_Target) {
        for (Iterator<String> t_Iterator = r_Target.iterator(); t_Iterator.hasNext(); ) {
            String t_Value = t_Iterator.next();
            if (equalsIgnoreCase(r_Source, t_Value))
                return true;
        }
        return false;
    }

    public static String targetEndStyle(String name) {
        return "</" + name + ">";
    }

    public static String valueToSetStyle(String value) {
        if (value == null)
            value = "";
        return "=\"" + value + "\"";
    }

    public static boolean equal(String s1, String s2) {
        if (s1 == s2)
            return true;
        if (s1 == null)
            s1 = "";
        if (s2 == null)
            s2 = "";
        s1 = s1.trim();
        s2 = s2.trim();
        if (s1.equals(s2))
            return true;
        return false;
    }

    public static String concat(Object... args) {
        StringBuffer buf = new StringBuffer();
        for (Object arg : args)
            buf.append(arg);
        return buf.toString();
    }

    public static String format(String s, Object[] params) {
        String message = s;
        if (message == null)
            return "";
        if (params != null && params.length > 0)
            message = (new MessageFormat(message)).format(params);
        return message;
    }

    public static boolean startsWithIgnoreCase(String str, String prefix) {
        if (str == null || prefix == null)
            return false;
        if (str.startsWith(prefix))
            return true;
        if (str.length() < prefix.length())
            return false;
        String lcStr = str.substring(0, prefix.length()).toLowerCase();
        String lcPrefix = prefix.toLowerCase();
        return lcStr.equals(lcPrefix);
    }

    public static boolean isNotEmpty(String str) {
        return (str != null && str.length() > 0);
    }

    public static String substringAfter(String str, String separator) {
        if (isEmpty(str))
            return str;
        if (separator == null)
            return "";
        int pos = str.indexOf(separator);
        if (pos == -1)
            return "";
        return str.substring(pos + separator.length());
    }

    public static String substringBefore(String str, String separator) {
        if (isEmpty(str) || separator == null)
            return str;
        if (separator.length() == 0)
            return "";
        int pos = str.indexOf(separator);
        if (pos == -1)
            return str;
        return str.substring(0, pos);
    }

    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0)
            return true;
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i)))
                return false;
        }
        return true;
    }

    public static String[] split(String str, char separatorChar) {
        return splitWorker(str, separatorChar, false);
    }

    public static String[] split(String str, String separatorChars) {
        return splitWorker(str, separatorChars, -1, false);
    }

    public static String[] split(String str, String separatorChars, int max) {
        return splitWorker(str, separatorChars, max, false);
    }

    public static boolean contains(String str, String searchStr) {
        if (str == null || searchStr == null)
            return false;
        return (str.indexOf(searchStr) >= 0);
    }

    public static String deleteWhitespace(String str) {
        if (isEmpty(str))
            return str;
        int sz = str.length();
        char[] chs = new char[sz];
        int count = 0;
        for (int i = 0; i < sz; i++) {
            if (!Character.isWhitespace(str.charAt(i)))
                chs[count++] = str.charAt(i);
        }
        if (count == sz)
            return str;
        return new String(chs, 0, count);
    }

    private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens) {
        if (str == null)
            return null;
        int len = str.length();
        if (len == 0)
            return EMPTY_STRING_ARRAY;
        List<String> list = new ArrayList();
        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match || preserveAllTokens) {
                    list.add(str.substring(start, i));
                    match = false;
                    lastMatch = true;
                }
                start = ++i;
                continue;
            }
            lastMatch = false;
            match = true;
            i++;
        }
        if (match || (preserveAllTokens && lastMatch))
            list.add(str.substring(start, i));
        return list.<String>toArray(new String[list.size()]);
    }

    public static String replace(String text, String repl, String with) {
        return replace(text, repl, with, -1);
    }

    public static String replace(String text, String repl, String with, int max) {
        if (text == null || isEmpty(repl) || with == null || max == 0)
            return text;
        StringBuffer buf = new StringBuffer(text.length());
        int start = 0, end = 0;
        while ((end = text.indexOf(repl, start)) != -1) {
            buf.append(text.substring(start, end)).append(with);
            start = end + repl.length();
            if (--max == 0)
                break;
        }
        buf.append(text.substring(start));
        return buf.toString();
    }

    public static String leftPad(String str, int size) {
        return leftPad(str, size, ' ');
    }

    public static String leftPad(String str, int size, char padChar) {
        if (str == null)
            return null;
        int pads = size - str.length();
        if (pads <= 0)
            return str;
        if (pads > 8192)
            return leftPad(str, size, String.valueOf(padChar));
        return padding(pads, padChar).concat(str);
    }

    private static String padding(int repeat, char padChar) {
        String pad = PADDING[padChar];
        if (pad == null)
            pad = String.valueOf(padChar);
        while (pad.length() < repeat)
            pad = pad.concat(pad);
        PADDING[padChar] = pad;
        return pad.substring(0, repeat);
    }

    public static String leftPad(String str, int size, String padStr) {
        if (str == null)
            return null;
        if (isEmpty(padStr))
            padStr = " ";
        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;
        if (pads <= 0)
            return str;
        if (padLen == 1 && pads <= 8192)
            return leftPad(str, size, padStr.charAt(0));
        if (pads == padLen)
            return padStr.concat(str);
        if (pads < padLen)
            return padStr.substring(0, pads).concat(str);
        char[] padding = new char[pads];
        char[] padChars = padStr.toCharArray();
        for (int i = 0; i < pads; i++)
            padding[i] = padChars[i % padLen];
        return (new String(padding)).concat(str);
    }

    public static String rightPad(String str, int size) {
        return rightPad(str, size, ' ');
    }

    public static String rightPad(String str, int size, char padChar) {
        if (str == null)
            return null;
        int pads = size - str.length();
        if (pads <= 0)
            return str;
        if (pads > 8192)
            return rightPad(str, size, String.valueOf(padChar));
        return str.concat(padding(pads, padChar));
    }

    public static String rightPad(String str, int size, String padStr) {
        if (str == null)
            return null;
        if (isEmpty(padStr))
            padStr = " ";
        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;
        if (pads <= 0)
            return str;
        if (padLen == 1 && pads <= 8192)
            return rightPad(str, size, padStr.charAt(0));
        if (pads == padLen)
            return str.concat(padStr);
        if (pads < padLen)
            return str.concat(padStr.substring(0, pads));
        char[] padding = new char[pads];
        char[] padChars = padStr.toCharArray();
        for (int i = 0; i < pads; i++)
            padding[i] = padChars[i % padLen];
        return str.concat(new String(padding));
    }

    public static boolean equals(String str1, String str2) {
        return (str1 == null) ? ((str2 == null)) : str1.equals(str2);
    }

    private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
        if (str == null)
            return null;
        int len = str.length();
        if (len == 0)
            return EMPTY_STRING_ARRAY;
        List<String> list = new ArrayList();
        int sizePlus1 = 1;
        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        if (separatorChars == null) {
            while (i < len) {
                if (Character.isWhitespace(str.charAt(i))) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        } else if (separatorChars.length() == 1) {
            char sep = separatorChars.charAt(0);
            while (i < len) {
                if (str.charAt(i) == sep) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        } else {
            while (i < len) {
                if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                    continue;
                }
                lastMatch = false;
                match = true;
                i++;
            }
        }
        if (match || (preserveAllTokens && lastMatch))
            list.add(str.substring(start, i));
        return list.<String>toArray(new String[list.size()]);
    }

    public static String removeStart(String str, String remove) {
        if (isEmpty(str) || isEmpty(remove))
            return str;
        if (str.startsWith(remove))
            return str.substring(remove.length());
        return str;
    }

    public static String removeEnd(String str, String remove) {
        if (isEmpty(str) || isEmpty(remove))
            return str;
        if (str.endsWith(remove))
            return str.substring(0, str.length() - remove.length());
        return str;
    }

    public static String remove(String str, String remove) {
        if (isEmpty(str) || isEmpty(remove))
            return str;
        return replace(str, remove, "", -1);
    }

    public static String remove(String str, char remove) {
        if (isEmpty(str) || str.indexOf(remove) == -1)
            return str;
        char[] chars = str.toCharArray();
        int pos = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != remove)
                chars[pos++] = chars[i];
        }
        return new String(chars, 0, pos);
    }

    public static String htmlFilter(String value) {
        if (value == null || value.length() == 0)
            return value;
        StringBuilder result = null;
        String filtered = null;
        for (int i = 0; i < value.length(); i++) {
            filtered = null;
            switch (value.charAt(i)) {
                case '<':
                    filtered = "&lt;";
                    break;
                case '>':
                    filtered = "&gt;";
                    break;
                case '&':
                    filtered = "&amp;";
                    break;
                case '"':
                    filtered = "&quot;";
                    break;
                case '\'':
                    filtered = "&#39;";
                    break;
            }
            if (result == null) {
                if (filtered != null) {
                    result = new StringBuilder(value.length() + 50);
                    if (i > 0)
                        result.append(value.substring(0, i));
                    result.append(filtered);
                }
            } else if (filtered == null) {
                result.append(value.charAt(i));
            } else {
                result.append(filtered);
            }
        }
        return (result == null) ? value : result.toString();
    }

    /**
     * * 判断一个对象是否非空
     *
     * @param object Object
     * @return true：非空 false：空
     */
    public static boolean isNotNull(Object object)
    {
        return !isNull(object);
    }

    /**
     * * 判断一个对象是否为空
     *
     * @param object Object
     * @return true：为空 false：非空
     */
    public static boolean isNull(Object object)
    {
        return object == null;
    }
}
